# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        
        l_path = self.maxDepth(root.left)
        r_path = self.maxDepth(root.right)

        return max(l_path,r_path)+1



# 递归法
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        ans = 0

        def f(node,cnt):
            if node is None:
                return 
            cnt += 1
            nonlocal ans
            ans = max(ans,cnt)
            f(node.left,cnt)
            f(node.right,cnt)
        
        f(root,0)
        return ans
            